<?php

/* Load the core theme framework. */
require_once( trailingslashit( TEMPLATEPATH ) . 'library/hybrid.php' );
$theme = new Hybrid();	
	
/* Load the theme-options framework. */
require_once( trailingslashit( TEMPLATEPATH ) . 'includes/includes.php' );
$includes = new CVIncludes();

/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'theme_setup');

/**
 * Theme setup function.  This function adds support for theme features and defines the default theme
 * actions and filters.
 */
function theme_setup() {
	
	/* Get action/filter hook prefix. */
	$prefix = hybrid_get_prefix();
	
	define('CV_OPTIONS', 'cv_'. $prefix . '_options'); //name of entry into database - will break DB if this has spaces!
	define('CV_BACKUPS', 'cv_'. $prefix . '_backups'); //name of entry into database - will break DB if this has spaces!
	

	/* Add theme support for core framework features. */
	add_theme_support( 'hybrid-core-menus', array( 'primary', 'secondary') );
	//add_theme_support( 'hybrid-core-sidebars', array( 'primary') );
	add_theme_support( 'hybrid-core-widgets' );
	add_theme_support( 'hybrid-core-shortcodes' );
	add_theme_support( 'hybrid-core-post-meta-box' );
	//add_theme_support( 'hybrid-core-drop-downs' );
	//add_theme_support( 'hybrid-core-template-hierarchy' );

	/* Add theme support for framework extensions. */
//	add_theme_support( 'post-stylesheets' );
//	add_theme_support( 'dev-stylesheet' );
//	add_theme_support( 'loop-pagination' );
	add_theme_support( 'get-the-image' );
//	add_theme_support( 'breadcrumb-trail' );
//	add_theme_support( 'cleaner-gallery' );

	add_theme_support( 'cvincludes-options');	
	add_theme_support( 'cvincludes-shortcode' );
	add_theme_support( 'cvincludes-socialmedia');
	add_theme_support( 'cvincludes-dropdowns', array('longsubs') );	
//	add_theme_support( 'cvincludes-slides', array('slide_width'=>960,'slide_height'=>307, 'image_width'=>960,'image_height'=>307, 'show_description'=>true, 'show_title'=>true), array('jquery_cycle', 'nivo_slider'));
	add_theme_support( 'cvincludes-portfolio', array('portfolio_type'=>'filterable'));
	add_theme_support( 'cvincludes-eztables' );

	add_theme_support( 'cvincludes-seo' );	
	$data = get_option(CV_OPTIONS);
	if($data['seo_enable_seo'] == true)
		add_theme_support( 'hybrid-core-seo' );			
	else {
		remove_theme_support( 'hybrid-core-seo' );	
	}
	

	/* Add theme support for WordPress features. */
	add_theme_support( 'automatic-feed-links' );	
	//add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
	
	/* Add the breadcrumb trail just after the container is open. */
	add_action( "{$prefix}_open_main", 'breadcrumb_trail' );
	
	/* Register sidebars. */
	add_action( 'init', 'theme_register_sidebars', 11 );

	/* Filter the breadcrumb trail arguments. */
	//add_filter( 'breadcrumb_trail_args', 'breadcrumb_trail_args' );	
	add_image_size( 'topstory-image-thumb', 88, 63, true);
	add_image_size( 'popular-image-thumb', 89, 64, true);
	
	//add_image_size( 'entry-image-thumb', 290, 200, true);
	
	add_image_size( 'four-column-thumb', 200, 120, true);
	add_image_size( 'three-column-thumb', 280, 130, true);
	add_image_size( 'two-column-thumb', 440, 210, true);

	add_action( 'add_meta_boxes', 'topstory_add_custom_box' );	
	add_action( 'save_post', 'topstory_save_postdata' );
}

/**
 * Custom breadcrumb trail arguments.
 */
function breadcrumb_trail_args( $args ) {

	/* Change the text before the breadcrumb trail. */
	$args['before'] = __( 'You are here:', hybrid_get_textdomain() );

	/* Return the filtered arguments. */
	return $args;
}

function theme_register_sidebars() {

	/* Register the widgets template sidebar. */
	register_sidebar(
		array(
			'id' => 'primary',
			'name' => __( 'Primary Sidebar', hybrid_get_textdomain() ),
			'description' => __( 'Primary sidebar.', hybrid_get_textdomain() ),
			'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">',
			'after_widget' => '</div></div>',
			'before_title' => '<span class="widget-title">',
			'after_title' => '</span>'
		)
	);
	
	/* Register the widgets template sidebar. */
	register_sidebar(
		array(
			'id' => 'sidebar-page',
			'name' => __( 'Page Sidebar', hybrid_get_textdomain() ),
			'description' => __( 'Page sidebar.', hybrid_get_textdomain() ),
			'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">',
			'after_widget' => '</div></div>',
			'before_title' => '<span class="widget-title">',
			'after_title' => '</span>'
		)
	);
	
	/* Register the widgets template sidebar. */
	register_sidebar(
		array(
			'id' => 'sidebar-subsidiary-1',
			'name' => __( 'Foot Sidebar #1', hybrid_get_textdomain() ),
			'description' => __( 'Foot Sidebar #1.', hybrid_get_textdomain() ),
			'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">',
			'after_widget' => '</div></div>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>'
		)
	);
	
	/* Register the widgets template sidebar. */
	register_sidebar(
		array(
			'id' => 'sidebar-subsidiary-2',
			'name' => __( 'Foot Sidebar #2', hybrid_get_textdomain() ),
			'description' => __( 'Foot Sidebar #2.', hybrid_get_textdomain() ),
			'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">',
			'after_widget' => '</div></div>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>'
		)
	);
	
	/* Register the widgets template sidebar. */
	register_sidebar(
		array(
			'id' => 'sidebar-subsidiary-3',
			'name' => __( 'Foot Sidebar #3', hybrid_get_textdomain() ),
			'description' => __( 'Foot Sidebar #3.', hybrid_get_textdomain() ),
			'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">',
			'after_widget' => '</div></div>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>'
		)
	);
	
}

/* Adds a box to the main column on the Post and Page edit screens */
	function topstory_add_custom_box() {
		add_meta_box( 
			'is_topstory',
			__( 'Push to Top-story', hybrid_get_textdomain() ),
			'topstory_inner_custom_box',
			'post',
			'side'
		);  
	}

	/* Prints the box content */
	function topstory_inner_custom_box( $post ) {

	  // Use nonce for verification
	  wp_nonce_field( basename( __FILE__ ), 'topstory_noncename' );

	  // The actual fields for data entry
	  echo '<label for="is_topstory">';
		   _e("Push?", hybrid_get_textdomain() );
	  echo '</label> ';
	  $checked = esc_attr( get_post_meta( $post->ID, 'is_topstory', true ) )=='on'?'checked="on"':'';
	  echo '<input type="checkbox" id="is_topstory" name="is_topstory" '. $checked . '/>';	
	}

	/* When the post is saved, saves our custom data */
	function topstory_save_postdata( $post_id ) {
	  // verify if this is an auto save routine. 
	  // If it is our form has not been submitted, so we dont want to do anything
	  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		  return;

	  // verify this came from the our screen and with proper authorization,
	  // because save_post can be triggered at other times

	  if ( !wp_verify_nonce( $_POST['topstory_noncename'], basename( __FILE__ ) ) )
		  return;

	  
	  // Check permissions
	  if ( 'post' == $_POST['post_type'] ) 
	  {
		 if ( !current_user_can( 'edit_post', $post_id ) )
			return;
	  }  

		$meta = array(
			'is_topstory' => strip_tags( $_POST['is_topstory'] )		
		);

		foreach ( $meta as $meta_key => $new_meta_value ) {

			/* Get the meta value of the custom field key. */
			$meta_value = get_post_meta( $post_id, $meta_key, true );

			/* If a new meta value was added and there was no previous value, add it. */
			if ( $new_meta_value && '' == $meta_value )
				add_post_meta( $post_id, $meta_key, $new_meta_value, true );

			/* If the new meta value does not match the old value, update it. */
			elseif ( $new_meta_value && $new_meta_value != $meta_value )
				update_post_meta( $post_id, $meta_key, $new_meta_value );

			/* If there is no new meta value but an old value exists, delete it. */
			elseif ( '' == $new_meta_value && $meta_value )
				delete_post_meta( $post_id, $meta_key, $meta_value );
		}
	}

?>